|
1
|
|
|
var Router = require('express') |
|
2
|
|
|
var passport = require('passport') |
|
3
|
|
|
const passportConfig = require('../config/passport') |
|
|
|
|
|
|
4
|
|
|
|
|
5
|
|
|
module.exports = class AuthController { |
|
6
|
|
|
|
|
7
|
|
|
route () { |
|
8
|
|
|
const router = new Router() |
|
9
|
|
|
// GET /auth/google |
|
10
|
|
|
// Use passport.authenticate() as route middleware to authenticate the |
|
11
|
|
|
// request. The first step in Google authentication will involve |
|
12
|
|
|
// redirecting the user to google.com. After authorization, Google |
|
13
|
|
|
// will redirect the user back to this application at /auth/google/callback |
|
14
|
|
|
router.get('/google', passport.authenticate('google', { |
|
15
|
|
|
scope: [ |
|
16
|
|
|
'https://www.googleapis.com/auth/plus.login', |
|
17
|
|
|
'https://www.googleapis.com/auth/plus.profile.emails.read'] |
|
18
|
|
|
})) |
|
19
|
|
|
|
|
20
|
|
|
// GET /auth/google/callback |
|
21
|
|
|
// Use passport.authenticate() as route middleware to authenticate the |
|
22
|
|
|
// request. If authentication fails, the user will be redirected back to the |
|
23
|
|
|
// login page. Otherwise, the primary route function function will be called, |
|
24
|
|
|
// which, in this example, will redirect the user to the home page. |
|
25
|
|
|
router.get('/google/callback', |
|
26
|
|
|
passport.authenticate('google', { |
|
27
|
|
|
successRedirect: '/auth/account', |
|
28
|
|
|
failureRedirect: '/auth/login' |
|
29
|
|
|
})) |
|
30
|
|
|
|
|
31
|
|
|
router.get('/account', passportConfig.ensureAuthenticated, function (req, res) { |
|
|
|
|
|
|
32
|
|
|
res.render('account', { user: req.user }) |
|
33
|
|
|
}) |
|
34
|
|
|
|
|
35
|
|
|
router.get('/login', function (req, res) { |
|
36
|
|
|
res.render('login', { user: req.user }) |
|
37
|
|
|
}) |
|
38
|
|
|
|
|
39
|
|
|
router.get('/logout', function (req, res) { |
|
40
|
|
|
req.logout() |
|
41
|
|
|
res.redirect('/') |
|
42
|
|
|
}) |
|
43
|
|
|
|
|
44
|
|
|
return router |
|
45
|
|
|
} |
|
46
|
|
|
} |
|
47
|
|
|
|